package com.example.a20112.ui_demo;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.a20112.ui_demo.beans.RegisterBean;
import com.example.a20112.ui_demo.listener.RetrofitListener;
import com.example.a20112.ui_demo.model.UserModel;

    public class RegisterActivity extends AppCompatActivity implements RetrofitListener<RegisterBean> {
    private EditText username,password1,password2,phonenumber,address,comment;
    private Button button_register;
    private ImageView back;
    private UserModel userModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
        initEvent();
    }

    private void initViews() {
        username = (EditText) findViewById(R.id.username);
        password1 = (EditText) findViewById(R.id.password1);
        button_register = (Button) findViewById(R.id.button_register);
        password2 = (EditText) findViewById(R.id.password2);
        address = (EditText) findViewById(R.id.address);
        phonenumber = (EditText) findViewById(R.id.phonenumber);
        comment = (EditText) findViewById(R.id.comment);
        back=(ImageView)findViewById(R.id.back);

    }

    private void initEvent() {
        button_register.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                if(password1.getText().toString().equals(password2.getText().toString())){
                    register();
                }
                else{
                    Toast.makeText(RegisterActivity.this, "请确认两次密码相同 ", Toast.LENGTH_SHORT).show();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    private void register(){
        userModel=new UserModel();
        userModel.register(username.getText().toString(),password1.getText().toString(),
                address.getText().toString(),
                phonenumber.getText().toString(),comment.getText().toString(),this);

    }


        public void onSuccess(RegisterBean success, int flag) {
        if(success.getSuccess().equals("0")) {
           Toast.makeText(RegisterActivity.this, "已经存在此用户", Toast.LENGTH_SHORT).show();
        }
        else if(success.getSuccess().equals("1")){
            Intent intent = new Intent();
            intent.putExtra("user",username.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
            Toast.makeText(RegisterActivity.this, "注册成功 ", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(RegisterActivity.this, "注册失败 ", Toast.LENGTH_SHORT).show();
        }
//        if(success.getSuccess().equals("0")){
//            Toast.makeText(RegisterActivity.this, "已经存在此用户", Toast.LENGTH_SHORT).show();
//        }
//        if(success.getSuccess().equals("1")) {
//          Intent intent = new Intent();
//          intent.putExtra("user",et_username.toString());
//          setResult(RESULT_OK,intent);
//          finish();;
//            Toast.makeText(RegisterActivity.this, "注册成功 ", Toast.LENGTH_SHORT).show();
//        }
//        else {
//            Toast.makeText(RegisterActivity.this, "注册失败 ", Toast.LENGTH_SHORT).show();
//        }
    }

    @Override
    public void onFail()  {
        Toast.makeText(RegisterActivity.this, "网络出错了", Toast.LENGTH_SHORT).show();

    }

}
